//
//  KLSGAlbumController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGAlbumController.h"
#import "KLSGAlbumCell.h"
#import "KLSGAlbumModel.h"
#import "KLSGBrifAndVoiceViewController.h"

@interface KLSGAlbumController ()

@end

@implementation KLSGAlbumController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNavigationBar];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"专辑";
    [self.tableView registerClass:[KLSGAlbumCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];

}

- (void)addNavigationBar
{
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemReply) target:self action:@selector(leftReturn)];
    self.navigationItem.leftBarButtonItem = left;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1];
}
- (void)leftReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataAlbumArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KLSGAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    KLSGAlbumModel *model = self.dataAlbumArray[indexPath.row];
    [cell.coverSmallImage sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage imageNamed:@"default_anchor_avatar"]];
    cell.titleLable.text = model.title;
    cell.tracksLable.text = [NSString stringWithFormat:@"%@集", model.tracks];
    CGFloat pTimes = [model.playTimes intValue];
    cell.playTimesLable.text = [NSString stringWithFormat:@"%.1f万", pTimes / 10000];
    
    CGFloat updateAt = [model.updatedAt floatValue];
    cell.updataAtLable.text = [NSString stringWithFormat:@"最后更新于%f天前", updateAt];
    [cell.collectButton addTarget:self action:@selector(actionWithCollect:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}
//  收藏按钮的点击事件
- (void)actionWithCollect:(KLSGCollectButton *)button
{
    KLSGAlbumCell *cell = (KLSGAlbumCell *)[button superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    KLSGAlbumModel *model = self.dataAlbumArray[indexPath.row];
    KLSGKindModel *kindModel = [[KLSGKindModel alloc] init];
    kindModel.albumCoverUrl290 = model.coverSmall;
    kindModel.title = model.title;
    kindModel.albumId = model.albumId;
    kindModel.playsCounts = model.playTimes;
    kindModel.tracksCounts = model.tracks;
    KLSGDB *dbManager = [[KLSGDB alloc] init];
    if ([dbManager queryKindWithAlbumId:model.albumId]) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alertVC animated:YES completion:nil];
        [self performSelector:@selector(dismissAlertVC:) withObject:alertVC afterDelay:0.4];
    } else {
        if ([dbManager insertWithKindModel:kindModel]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alertVC animated:YES completion:nil];
            [self performSelector:@selector(dismissAlertVC:) withObject:alertVC afterDelay:0.4];
        } else {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"不可收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alertVC animated:YES completion:nil];
            [self performSelector:@selector(dismissAlertVC:) withObject:alertVC afterDelay:0.4];
        }
    }
    
    }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGBrifAndVoiceViewController *VC = [[KLSGBrifAndVoiceViewController alloc] init];
    KLSGAlbumModel *model = self.dataAlbumArray[indexPath.row];
    VC.albumIdString = [NSString stringWithFormat:@"%@", model.albumId];
    KLSGKindModel *kindModel = [[KLSGKindModel alloc] init];
    kindModel.albumCoverUrl290 = model.coverSmall;
    kindModel.albumId = model.albumId;
    kindModel.title = model.title;
    kindModel.playsCounts = model.playTimes;
    kindModel.tracksCounts = model.tracks;
    VC.model = kindModel;
    [self.navigationController pushViewController:VC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark -alertVC 
- (void)dismissAlertVC:(UIAlertController *)alertVC
{
    [alertVC dismissViewControllerAnimated:YES completion:nil];
}

@end
